<?php

namespace app\model;

use think\Model;
use think\facade\Log;

class HonorModel extends Model
{
    protected $table = 'honors';

    // 关联荣誉的成员
    public function member()
    {
        return $this->belongsTo(MemberModel::class, 'user_id');
    }

    // 关联荣誉的审核者
    public function verifier()
    {
        return $this->belongsTo(UserModel::class, 'verified_by');
    }

    // 数据验证
    public static function validateData($data)
    {
        $validate = new \think\Validate([
            'user_id' => 'require|number',
            'type' => 'require|in:1,2,3', // 1:证书, 2:奖项, 3:认证
            'name' => 'require|max:255',
            'file_path' => 'require|max:255',
        ]);

        return $validate->check($data);
    }

    // 审核荣誉记录
    public static function verifyHonor($id, $status, $userId)
    {
        try {
            $honor = self::find($id);
            if (!$honor) {
                throw new \Exception('荣誉记录不存在');
            }

            $honor->status = $status;
            $honor->verified_by = $userId;
            $honor->verified_at = time();
            $honor->save();

            return true;
        } catch (\Exception $e) {
            Log::error("审核荣誉失败: " . $e->getMessage());
            return false;
        }
    }
}
